import animationHelper from '../../utils/animation';
import api from '../../utils/api';
import request from '../../utils/request';
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    type: String,
    status: String,
    totalAmount: String,//总金额
    allChoose: Boolean,//是否全选
    btnDisabled: {
      type: Boolean,
      value: false
    },//立即结算是否可以点击
    num: Number,
    applyRefundReason: String,
    applyRefundRemark: String,
    orderInfo: Object,
    orderStatus: String
  },

  /**
   * 组件的初始数据
   */
  data: {
    reasonList: [
      {
        id: 0,
        text: '不想要了',
        checked: false,
      },
      {
        id: 1,
        text: '地址信息填写错误',
        checked: false
      },
      {
        id: 2,
        text: '商品错选/多选',
        checked: false
      },
      {
        id: 3,
        text: '商品降价',
        checked: false
      },
      {
        id: 4,
        text: '没用/少用/错用优惠',
        checked: false
      },
    ] as Reason[],
    showDialog: false as boolean,
    animation: null as WechatMiniprogram.Animation | null,
    animationData: {} as any,
    sendDialog: false as boolean,
    company: '' as string,//快递公司
    sendNum: "" as string,//运单号
  },
  lifetimes: {
    attached() {

    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    //去购物车
    toCart(): void {
      wx.navigateTo({
        url: "/pages/cart/cart"
      })
    },
    // 订单确认
    toNext(): void {
      if (this.data.type == 'cart' && !this.data.btnDisabled) {
        this.triggerEvent("settlement")
      } else if (this.data.type == 'order') {
        this.triggerEvent("createOrder")
      }
    },
    // 点击取消订单
    showDialog(): void {
      const animation = animationHelper.createAnimation(500, 'ease');
      this.setData({
        animation: animation
      });
      animationHelper.showAnimation(animation, 589, this.setData.bind(this), 'showDialog');
    },
    // 关闭弹窗
    closeTips(): void {
      const animation = this.data.animation;
      animationHelper.closeAnimation(animation, 589, this.setData.bind(this), 'showDialog');
    },
    // 确认取消订单
    toRefund(): void {
      const { reasonList } = this.data
      // 判断原因是否选择
      const index = reasonList.findIndex(item => {
        return item.checked
      })
      if (index < 0) {
        wx.showToast({
          title: "请选择原因",
          icon: "error"
        })
        return
      }
      this.closeTips();
      this.triggerEvent("confirm", { reason: reasonList[index].text })
    },
    // 退款原因选择
    changeStatus(e: any): void {
      const { reasonList } = this.data
      const index = reasonList.findIndex(item => {
        return item.id === e.currentTarget.dataset.id
      })
      reasonList.map(item => {
        item.checked = false
      })
      reasonList[index].checked = true
      this.setData({
        reasonList
      })
    },
    // 全选
    selectAll(): void {
      this.triggerEvent("changeStatus")
    },
    // 立即购买
    buyNow(): void {
      this.triggerEvent("buy")
    },
    // 加入购物车
    addCart(): void {
      this.triggerEvent("addCart")
    },
    // 订单详情点击立即支付
    toPay(): void {
      this.triggerEvent("toPay")
    },
    // 活动商品立即抢购
    activityGoodsBuy(): void {
      this.triggerEvent("buy")
    },
    // 去退款页面
    toRefundDetail(): void {
      this.triggerEvent("toRefundDetail")
    },
    // 去物流
    toLogistics(): void {
      this.triggerEvent("toLogistics")
    },
    // 展示寄货dialog
    showSendDialog(): void {
      const animation = animationHelper.createAnimation(500, 'ease');
      this.setData({
        animation: animation
      });
      animationHelper.showAnimation(animation, 454, this.setData.bind(this), 'sendDialog');
    },
    closeSend(): void {
      const animation = this.data.animation;
      animationHelper.closeAnimation(animation, 454, this.setData.bind(this), 'sendDialog');
    },
    // 处理输入
    handleCompany(e: any): void {
      this.setData({
        company: e.detail.value
      })
    },
    handleNum(e: any): void {
      this.setData({
        sendNum: e.detail.value
      })
    },
    // 填写完成
    fillDone(): void {
      const { company, sendNum } = this.data
      if (!company) {
        wx.showToast({
          title: "请填写快递公司",
          icon: "error"
        })
        return
      }
      if (!sendNum) {
        wx.showToast({
          title: "请填写运单号",
          icon: "error"
        })
        return
      }
      this.closeSend();
      this.triggerEvent("sendGoods", { shipCompany: company, shipNo: sendNum })
    },
    // 确认收货
    complete(): void {
      this.triggerEvent("complete")
    },
    // 取消申请
    cancelRefund(): void {
      this.closeSend();
      this.triggerEvent("cancelRefund")
    },
    showConfirmModel(): void {
      wx.showModal({
        title: "确认删除",
        content: "是否确认删除此订单",
        confirmColor: "#EF0C26",
        success: (res) => {
          if (res.confirm) {
            this.triggerEvent("delete")
          }
        }
      })
    }
  }
})